from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
import sys


def on_toggle(arg):
    print("选中：", arg)


def on_group_toggle(btn: QRadioButton):
    print(btn, btn.isChecked())


if __name__ == '__main__':
    # 1.创建应用程序
    app = QApplication(sys.argv)

    # 2.创建窗口
    w = QWidget()

    w.resize(400, 300)

    # 修改窗口标题
    w.setWindowTitle('单选框')

    btn1 = QRadioButton("单选钮1")
    btn2 = QRadioButton("单选钮2")
    btn1.setChecked(True)

    # 添加两个单选钮到QButtonGroup，并添加选中事件
    group = QButtonGroup(w)
    group.addButton(btn1)
    group.addButton(btn2)
    group.buttonToggled.connect(on_group_toggle)

    # 添加到w中
    layout = QVBoxLayout()
    layout.addWidget(btn1)
    layout.addWidget(btn2)
    w.setLayout(layout)

    # 3.显示窗口
    w.show()

    # 4.等待窗口停止
    sys.exit(app.exec())
